package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Floor;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.domain.RoomType;
import com.zzyl.nursing.mapper.FloorMapper;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.mapper.RoomTypeMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.BedVo;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.DeviceVo;
import com.zzyl.nursing.vo.RoomVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private FloorMapper floorMapper;

    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    String IOT_DEVICE_LAST_DATA = "IOT_DEVICE_LAST_DATA:";

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    @Override
    public RoomVo oneRoomId(Long roomId) {
        return roomMapper.getOneRoom(roomId);
    }

    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);

        Floor floor = floorMapper.selectFloorById(floorId);


        for (RoomVo roomVo : roomVos) {
            LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<Room>().eq(Room::getCode, roomVo.getCode());
            Room room = roomMapper.selectOne(wrapper);

            roomVo.setFloorName(floor.getName());
            roomVo.setFloorId(floorId.toString());
            roomVo.setRoomId(roomVo.getId().toString());
            roomVo.setRoomId(room.getId().toString());
            roomVo.setRoomName(room.getTypeName());

            RoomType roomType = roomTypeMapper.selectOne(new LambdaQueryWrapper<RoomType>().eq(RoomType::getName, room.getTypeName()));
            roomVo.setPrice(String.valueOf(roomType.getPrice()));

            for (BedVo bedVo : roomVo.getBedVoList()) {
                List<DeviceVo> deviceVos = bedVo.getDeviceVos();
                if (deviceVos != null) {
                    for (DeviceVo deviceVo : deviceVos) {
                        String iotId = deviceVo.getIotId();
                        Object result = redisTemplate.opsForValue().get(IOT_DEVICE_LAST_DATA + iotId);
                        String jsonStr = JSONUtil.toJsonStr(result);
                        List<DeviceDataVo> list = JSONUtil.toList(jsonStr, DeviceDataVo.class);
                        deviceVo.setDeviceDataVos(list);
                    }
                }
            }

            List<DeviceVo> deviceVos = roomVo.getDeviceVos();
            for (DeviceVo deviceVo : deviceVos) {
                Object result = redisTemplate.opsForValue().get(IOT_DEVICE_LAST_DATA + deviceVo.getIotId());
                String jsonStr = JSONUtil.toJsonStr(result);
                List<DeviceDataVo> list = JSONUtil.toList(jsonStr, DeviceDataVo.class);
                deviceVo.setDeviceDataVos(list);
            }
        }
        return roomVos;
    }
}
